import json
import re
import csv
import requests

# https://datacenter-web.eastmoney.com/api/data/v1/get?callback=datatable2391729&columns=REPORT_DATE%2CTIME%2CDOMESTICL_PRODUCT_BASE%2CFIRST_PRODUCT_BASE%2CSECOND_PRODUCT_BASE%2CTHIRD_PRODUCT_BASE%2CSUM_SAME%2CFIRST_SAME%2CSECOND_SAME%2CTHIRD_SAME&pageNumber=1&pageSize=20&sortColumns=REPORT_DATE&sortTypes=-1&source=WEB&client=WEB&reportName=RPT_ECONOMY_GDP&p=1&pageNo=1&pageNum=1&_=1743590094182
# https://datacenter-web.eastmoney.com/api/data/v1/get?callback=datatable4781513&columns=REPORT_DATE%2CTIME%2CDOMESTICL_PRODUCT_BASE%2CFIRST_PRODUCT_BASE%2CSECOND_PRODUCT_BASE%2CTHIRD_PRODUCT_BASE%2CSUM_SAME%2CFIRST_SAME%2CSECOND_SAME%2CTHIRD_SAME&pageNumber=2&pageSize=20&sortColumns=REPORT_DATE&sortTypes=-1&source=WEB&client=WEB&reportName=RPT_ECONOMY_GDP&p=2&pageNo=2&pageNum=2&_=1743590094183
# https://datacenter-web.eastmoney.com/api/data/v1/get?callback=datatable5586269&columns=REPORT_DATE%2CTIME%2CDOMESTICL_PRODUCT_BASE%2CFIRST_PRODUCT_BASE%2CSECOND_PRODUCT_BASE%2CTHIRD_PRODUCT_BASE%2CSUM_SAME%2CFIRST_SAME%2CSECOND_SAME%2CTHIRD_SAME&pageNumber=3&pageSize=20&sortColumns=REPORT_DATE&sortTypes=-1&source=WEB&client=WEB&reportName=RPT_ECONOMY_GDP&p=3&pageNo=3&pageNum=3&_=1743590094184
# https://datacenter-web.eastmoney.com/api/data/v1/get?callback=datatable4894774&columns=REPORT_DATE%2CTIME%2CDOMESTICL_PRODUCT_BASE%2CFIRST_PRODUCT_BASE%2CSECOND_PRODUCT_BASE%2CTHIRD_PRODUCT_BASE%2CSUM_SAME%2CFIRST_SAME%2CSECOND_SAME%2CTHIRD_SAME&pageNumber=4&pageSize=20&sortColumns=REPORT_DATE&sortTypes=-1&source=WEB&client=WEB&reportName=RPT_ECONOMY_GDP&p=4&pageNo=4&pageNum=4&_=1743590094185
with open('GDP.csv', 'a', newline='',encoding='utf-8-sig') as csvFile:
    csvWriter = csv.writer(csvFile)
    for page in range(1,5):
        url=f"https://datacenter-web.eastmoney.com/api/data/v1/get?callback=datatable9141227&columns=REPORT_DATE%2CTIME%2CDOMESTICL_PRODUCT_BASE%2CFIRST_PRODUCT_BASE%2CSECOND_PRODUCT_BASE%2CTHIRD_PRODUCT_BASE%2CSUM_SAME%2CFIRST_SAME%2CSECOND_SAME%2CTHIRD_SAME&pageNumber={page}&pageSize=20&sortColumns=REPORT_DATE&sortTypes=-1&source=WEB&client=WEB&reportName=RPT_ECONOMY_GDP&p=2&pageNo=2&pageNum=2&_=1743591529516"
        res = requests.get(url)
        GDP = res.text.replace('datatable9141227','')
        all_GDP = re.search(r'"data":\[(.*?)\]',GDP,re.S)
        json_GDP = json.loads('{' + all_GDP.group() + '}')
        for one_GDP in json_GDP['data']:
            print(f"正在爬取第{one_GDP['TIME']}季度数据...")
            csvWriter.writerow([f"季度：{one_GDP['TIME']}，国内生产总值(绝对值(亿元))：{one_GDP['DOMESTICL_PRODUCT_BASE']}，第一产业(绝对值(亿元))：{one_GDP['FIRST_PRODUCT_BASE']}，第二产业(绝对值(亿元))：{one_GDP['SECOND_PRODUCT_BASE']}，第三产业(绝对值(亿元))：{one_GDP['THIRD_PRODUCT_BASE']}"])



import logging

# 获取实例
logger = logging.getLogger(__name__)
# 设置日志级别
logger.setLevel(logging.DEBUG)


# 设置日志格式
common_formatter = logging.Formatter('%(name)s--%(levelno)s--%(lineno)d--%(message)s--%(asctime)s')


# 设置日志处理句柄
stream_handler = logging.StreamHandler()
stream_handler.setLevel(logging.DEBUG)
stream_handler.setFormatter(common_formatter)

# 设置文件处理句柄
file_handler = logging.FileHandler(f"{__name__}.log", encoding="utf8")
file_handler.setLevel(logging.WARNING)
file_handler.setFormatter(common_formatter)


# 添加流句柄
logger.addHandler(stream_handler)
logger.addHandler(file_handler)


# 输出
logger.error("出错了")




